package com.cc.utils;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import java.text.MessageFormat;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DESTool {

public static void main(String args[]) throws Exception{  
   //System.out.println(EncryptAsDoNet("1100603024816_2010-6-3_12","26451d8f"));  
    
	 String key = "#U)R(9@J"; 
	 byte[] s = key.getBytes();
	 for(int i=0; i<s.length;i++) {
		 System.out.println(s[i]);
	 }
}  


public static String DecryptDoNet(String message, String key) throws Exception {     
    //base64 + des 解密.net 加密传来串  
      //byte[] bytesrc = convertHexString(message); //不用base64 的方式  
	  BASE64Decoder base64Decoder = new BASE64Decoder();  
      byte[] bytesrc=base64Decoder.decodeBuffer(message);  
      Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");     
      DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("ASCII"));     
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");     
      SecretKey secretKey = keyFactory.generateSecret(desKeySpec);     
      IvParameterSpec iv = new IvParameterSpec(key.getBytes("ASCII"));     
      cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);     
      byte[] retByte = cipher.doFinal(bytesrc);     
        
      return new String(retByte);     
}     
public static String EncryptAsDoNet(String message) throws Exception {   
	 byte[] DESkey = "#U)R(9@J".getBytes();// 设置密钥  
     DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数  
     SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂  
     Key key = keyFactory.generateSecret(keySpec);// 得到密钥对象  

     Cipher enCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");// 得到加密对象Cipher  
     //// 设置工作模式为加密模式，给出密钥和向量  
     enCipher.init(Cipher.ENCRYPT_MODE, key);
     byte[] pasByte = enCipher.doFinal(message.getBytes("utf-8"));  
     BASE64Encoder base64Encoder = new BASE64Encoder();  
     return base64Encoder.encode(pasByte);    
          
}    
}



